package com.koo.framework.config;

import com.koo.modules.quartz.server.impl.AdminStoreThirdDataServiceImpl;
import com.koo.modules.quartz.server.impl.BJAThirdDataServiceImpl;
import com.koo.modules.quartz.server.impl.CXThirdDataServiceImpl;
import com.koo.modules.quartz.server.impl.GYThirdDataServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
/**
 * 使用quartz的配置方式执行,可以在配置文件中指定表达式
 * -- 开启排程:全部以行注释 开始的行 的注释 全部取消
 * @author charlin
 */
@Slf4j
@Configuration
public class QuartzConfig {

    @Value("${third.gy_pull_store_data_cron}")
    private String gyPullStoreDataCron;
    
    /**
     * 调度工厂, 执行所有的定时任务
     * @param triggers(spring 自动注入)
     * @return
     */
    @Bean
    public SchedulerFactoryBean schedulerFactory(Trigger[] triggers){
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        //延时启动,避免系统为完全启动就开始执行任务
        bean.setStartupDelay(15);
        //覆盖已经存在的任务
        bean.setOverwriteExistingJobs(true);
        //设置触发器
        bean.setTriggers(triggers);
        return bean;
    }
    /* 广缘同步门店排程  */
    @Bean(name = "GY_storeDataTransport_Trigger")
    public CronTriggerFactoryBean GY_storeDataTransport_Trigger(@Qualifier("GY_storeDataTransport_Job") MethodInvokingJobDetailFactoryBean detailFactoryBean){
        return createCronTrigger(detailFactoryBean, gyPullStoreDataCron);
    }
    @Bean(name = "GY_storeDataTransport_Job")
    public MethodInvokingJobDetailFactoryBean GY_storeDataTransport_Job(GYThirdDataServiceImpl GY){
        return createDetailFactoryBean(GY, "storeDataTransport");
    }
    /* 广缘同步商品排程 */
    @Bean(name = "GY_productDataTransport_Trigger")
    public CronTriggerFactoryBean GY_productDataTransport_Trigger(@Qualifier("GY_productDataTransport_Job") MethodInvokingJobDetailFactoryBean detailFactoryBean){
        return createCronTrigger(detailFactoryBean, ThirdPropFileConfig.getProperty("GY_PULL_PRODUCT_DATA_CRON"));
    }
    @Bean(name = "GY_productDataTransport_Job")
    public MethodInvokingJobDetailFactoryBean GY_productDataTransport_Job(GYThirdDataServiceImpl GY){
        return createDetailFactoryBean(GY, "productDataTransport");
    }
    /* 广缘同步出库单排程 */
    @Bean(name = "GY_outOrderDataTransport_Trigger")
    public CronTriggerFactoryBean GY_outOrderDataTransport_Trigger(@Qualifier("GY_outOrderDataTransport_Job") MethodInvokingJobDetailFactoryBean detailFactoryBean){
        return createCronTrigger(detailFactoryBean, ThirdPropFileConfig.getProperty("GY_PULL_OUTORDER_DATA_CRON"));
    }
    @Bean(name = "GY_outOrderDataTransport_Job")
    public MethodInvokingJobDetailFactoryBean GY_outOrderDataTransport_Job(GYThirdDataServiceImpl GY){
        return createDetailFactoryBean(GY, "outOrderDataTransport");
    }
    /* 广缘转换出库单排程 */
    @Bean(name = "GY_outOrderConvert_Trigger")
    public CronTriggerFactoryBean GY_outOrderConvert_Trigger(@Qualifier("GY_outOrderConvert_Job") MethodInvokingJobDetailFactoryBean detailFactoryBean){
        return createCronTrigger(detailFactoryBean, ThirdPropFileConfig.getProperty("GY_CONVERT_OUTORDER_DATA_CRON"));
    }
    @Bean(name = "GY_outOrderConvert_Job")
    public MethodInvokingJobDetailFactoryBean GY_outOrderConvert_Job(GYThirdDataServiceImpl GY){
        return createDetailFactoryBean(GY, "convertOutOrderData");
    }
    
    // ------------------------
    // --                    --
    // ------------------------
    
//    /* BJA同步门店排程  */
    @Bean(name = "BJA_storeDataTransport_Trigger")
    public CronTriggerFactoryBean BJA_storeDataTransport_Trigger(@Qualifier("BJA_storeDataTransport_Job") MethodInvokingJobDetailFactoryBean detailFactoryBean){
        return createCronTrigger(detailFactoryBean, ThirdPropFileConfig.getProperty("BJA_PULL_STORE_DATA_CRON"));
    }
    @Bean(name = "BJA_storeDataTransport_Job")
    public MethodInvokingJobDetailFactoryBean BJA_storeDataTransport_Job(BJAThirdDataServiceImpl BJA){
        return createDetailFactoryBean(BJA, "storeDataTransport");
    }
    /* BJA同步商品排程 */
    @Bean(name = "BJA_productDataTransport_Trigger")
    public CronTriggerFactoryBean BJA_productDataTransport_Trigger(@Qualifier("BJA_productDataTransport_Job") MethodInvokingJobDetailFactoryBean detailFactoryBean){
        return createCronTrigger(detailFactoryBean, ThirdPropFileConfig.getProperty("BJA_PULL_PRODUCT_DATA_CRON"));
    }
    @Bean(name = "BJA_productDataTransport_Job")
    public MethodInvokingJobDetailFactoryBean BJA_productDataTransport_Job(BJAThirdDataServiceImpl BJA){
        return createDetailFactoryBean(BJA, "productDataTransport");
    }
    /* BJA同步出库单排程 */
    @Bean(name = "BJA_outOrderDataTransport_Trigger")
    public CronTriggerFactoryBean BJA_outOrderDataTransport_Trigger(@Qualifier("BJA_outOrderDataTransport_Job") MethodInvokingJobDetailFactoryBean detailFactoryBean){
        return createCronTrigger(detailFactoryBean, ThirdPropFileConfig.getProperty("BJA_PULL_OUTORDER_DATA_CRON"));
    }
    @Bean(name = "BJA_outOrderDataTransport_Job")
    public MethodInvokingJobDetailFactoryBean BJA_outOrderDataTransport_Job(BJAThirdDataServiceImpl BJA){
        return createDetailFactoryBean(BJA, "outOrderDataTransport");
    }
    /* BJA转换出库单排程 */
    @Bean(name = "BJA_outOrderConvert_Trigger")
    public CronTriggerFactoryBean BJA_outOrderConvert_Trigger(@Qualifier("BJA_outOrderConvert_Job") MethodInvokingJobDetailFactoryBean detailFactoryBean){
        return createCronTrigger(detailFactoryBean, ThirdPropFileConfig.getProperty("BJA_CONVERT_OUTORDER_DATA_CRON"));
    }
    @Bean(name = "BJA_outOrderConvert_Job")
    public MethodInvokingJobDetailFactoryBean BJA_outOrderConvert_Job(BJAThirdDataServiceImpl BJA){
        return createDetailFactoryBean(BJA, "convertOutOrderData");
    }
    
    /* BJA上传会员数据排程 */
    @Bean(name = "BJA_uploadMemberData_Trigger")
    public CronTriggerFactoryBean BJA_uploadMemberData_Trigger(@Qualifier("BJA_uploadMemberData_Job") MethodInvokingJobDetailFactoryBean detailFactoryBean){
        return createCronTrigger(detailFactoryBean, ThirdPropFileConfig.getProperty("BJA_UPLOAD_MEMBER_DATA_CRON"));
    }
    @Bean(name = "BJA_uploadMemberData_Job")
    public MethodInvokingJobDetailFactoryBean BJA_uploadMemberData_Job(BJAThirdDataServiceImpl BJA){
        return createDetailFactoryBean(BJA, "uploadMemberData");
    }
    /* BJA上传销售数据排程 */
    @Bean(name = "BJA_uploadSaleData_Trigger")
    public CronTriggerFactoryBean BJA_uploadSaleData_Trigger(@Qualifier("BJA_uploadSaleData_Job") MethodInvokingJobDetailFactoryBean detailFactoryBean){
        return createCronTrigger(detailFactoryBean, ThirdPropFileConfig.getProperty("BJA_UPLOAD_SALE_DATA_CRON"));
    }
    @Bean(name = "BJA_uploadSaleData_Job")
    public MethodInvokingJobDetailFactoryBean BJA_uploadSaleData_Job(BJAThirdDataServiceImpl BJA){
        return createDetailFactoryBean(BJA, "uploadSaleData");
    }
    /* BJA上传库存数据排程 */
    @Bean(name = "BJA_uploadStockData_Trigger")
    public CronTriggerFactoryBean BJA_uploadStockData_Trigger(@Qualifier("BJA_uploadStockData_Job") MethodInvokingJobDetailFactoryBean detailFactoryBean){
        return createCronTrigger(detailFactoryBean, ThirdPropFileConfig.getProperty("BJA_UPLOAD_STOCK_DATA_CRON"));
    }
    @Bean(name = "BJA_uploadStockData_Job")
    public MethodInvokingJobDetailFactoryBean BJA_uploadStockData_Job(BJAThirdDataServiceImpl BJA){
        return createDetailFactoryBean(BJA, "uploadStockData");
    }
    
    
    
    
    
    // ------------------------
    // --                    --
    // ------------------------
    /* 同步门店信息到监控端 */
    @Bean(name = "ADMIN_storeDataToAdminTransport_Trigger")
    public CronTriggerFactoryBean ADMIN_storeDataToAdminTransport_Trigger(@Qualifier("ADMIN_storeDataToAdminTransport_Job") MethodInvokingJobDetailFactoryBean detailFactoryBean){
    return createCronTrigger(detailFactoryBean, ThirdPropFileConfig.getProperty("ADMIN_STORE_TRANSPORT_CRON"));
    }
    @Bean(name = "ADMIN_storeDataToAdminTransport_Job")
    public MethodInvokingJobDetailFactoryBean ADMIN_storeDataToAdminTransport_Job(AdminStoreThirdDataServiceImpl admin){
        return createDetailFactoryBean(admin, "storeDataToAdminTransport");
    }
    
    // ------------------------
    // --                    --
    // ------------------------

    /* 辰星同步门店排程  */
    @Bean(name = "CX_storeDataTransport_Trigger")
    public CronTriggerFactoryBean CX_storeDataTransport_Trigger(@Qualifier("CX_storeDataTransport_Job") MethodInvokingJobDetailFactoryBean detailFactoryBean){
        return createCronTrigger(detailFactoryBean, ThirdPropFileConfig.getProperty("CX_PULL_STORE_DATA_CRON"));
    }
    @Bean(name = "CX_storeDataTransport_Job")
    public MethodInvokingJobDetailFactoryBean CX_storeDataTransport_Job(CXThirdDataServiceImpl CX){
        return createDetailFactoryBean(CX, "storeDataTransport");
    }
    /* 辰星同步出库单排程 */
    @Bean(name = "CX_outOrderDataTransport_Trigger")
    public CronTriggerFactoryBean CX_outOrderDataTransport_Trigger(@Qualifier("CX_outOrderDataTransport_Job") MethodInvokingJobDetailFactoryBean detailFactoryBean){
        return createCronTrigger(detailFactoryBean, ThirdPropFileConfig.getProperty("CX_PULL_OUTORDER_DATA_CRON"));
    }
    @Bean(name = "CX_outOrderDataTransport_Job")
    public MethodInvokingJobDetailFactoryBean CX_outOrderDataTransport_Job(CXThirdDataServiceImpl CX){
        return createDetailFactoryBean(CX, "outOrderDataTransport");
    }
    /* 辰星转换出库单排程 */
    @Bean(name = "CX_outOrderConvert_Trigger")
    public CronTriggerFactoryBean CX_outOrderConvert_Trigger(@Qualifier("CX_outOrderConvert_Job") MethodInvokingJobDetailFactoryBean detailFactoryBean){
        return createCronTrigger(detailFactoryBean, ThirdPropFileConfig.getProperty("CX_CONVERT_OUTORDER_DATA_CRON"));
    }
    @Bean(name = "CX_outOrderConvert_Job")
    public MethodInvokingJobDetailFactoryBean CX_outOrderConvert_Job(CXThirdDataServiceImpl CX){
        return createDetailFactoryBean(CX, "convertOutOrderData");
    }

    // ------------------------
    // --                    --
    // ------------------------

    /**
     * 通用创建触发器
     * @param detailFactoryBean
     * @param cronExpression
     * @return
     */
    private CronTriggerFactoryBean createCronTrigger(MethodInvokingJobDetailFactoryBean detailFactoryBean, String cronExpression){
        CronTriggerFactoryBean bean = new CronTriggerFactoryBean();
        bean.setJobDetail(detailFactoryBean.getObject());
        bean.setCronExpression(cronExpression);
        return bean;
    }
    
    /**
     * 通用创建job
     * @param oneService
     * @param methodName
     * @return
     */
    private MethodInvokingJobDetailFactoryBean createDetailFactoryBean(Object oneService, String methodName){
        MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean();
        bean.setTargetObject(oneService);
        bean.setTargetMethod(methodName);
        bean.setConcurrent(false);//不同时执行
        return bean;
    }

}
